// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GroupRegionAdapter.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Enables use of a Ribbon control as a Prism region.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Common.Adapters
{
    using System.Collections.Specialized;
    using System.Windows;

    using Fluent;

    using Microsoft.Practices.Prism.Regions;

    /// <summary>
    /// Enables use of a Ribbon control as a Prism region.
    /// </summary>
    /// <remarks> See Developer's Guide to Microsoft Prism (Ver. 4), p. 189.</remarks>
    public class GroupRegionAdapter : RegionAdapterBase<RibbonGroupBox>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="GroupRegionAdapter"/> class.
        /// </summary>
        /// <param name="behaviorFactory">
        /// Allows the registration of the default set of RegionBehaviors.
        /// </param>
        public GroupRegionAdapter(IRegionBehaviorFactory behaviorFactory)
            : base(behaviorFactory)
        {
        }

        /// <summary>
        /// Adapts a WPF control to serve as a Prism IRegion.
        /// </summary>
        /// <param name="region">The new region being used.</param>
        /// <param name="regionTarget">The WPF control to adapt.</param>
        protected override void Adapt(IRegion region, RibbonGroupBox regionTarget)
        {
            region.Views.CollectionChanged += (sender, e) =>
            {
                switch (e.Action)
                {
                    case NotifyCollectionChangedAction.Add:
                        foreach (FrameworkElement element in e.NewItems)
                        {
                            var value = element.GetValue(Helpers.IndexHelper.IndexProperty);
                            if (value != null)
                            {
                                int controlInsertIndex = int.Parse(value.ToString());
                                int insertIndex = -1;
                                int i = 0;
                                foreach (FrameworkElement control in regionTarget.Items)
                                {
                                    var itemIndex = control.GetValue(Helpers.IndexHelper.IndexProperty);
                                    if (itemIndex != null)
                                    {
                                        int itemIndexValue = int.Parse(itemIndex.ToString());
                                        if (itemIndexValue > controlInsertIndex)
                                        {
                                            insertIndex = i;
                                            break;
                                        }

                                        i++;
                                    }
                                }

                                if (insertIndex >= 0 && insertIndex < regionTarget.Items.Count)
                                {
                                    regionTarget.Items.Insert(insertIndex, element);
                                    continue;
                                }
                            }

                            regionTarget.Items.Add(element);
                        }

                        break;

                    case NotifyCollectionChangedAction.Remove:
                        foreach (UIElement elementLoopVariable in e.OldItems)
                        {
                            var element = elementLoopVariable;
                            if (regionTarget.Items.Contains(element))
                            {
                                regionTarget.Items.Remove(element);
                            }
                        }

                        break;
                }
            };
        }

        /// <summary>
        /// Creates the region.
        /// </summary>
        /// <returns>Region object</returns>
        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }
    }
}